package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.User;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class UserDao {
	
	DBHelp<User> db = new DBHelp<User>();

	public void save(User user) {
		
		String sql = "insert into t_employee(id,username,password) values(?,?,?)";
		db.executeSQL(sql, user.getId(),user.getUsername(),user.getPassword());
	}

	public User findByUsernamePassword(String name, String password) {
		// TODO Auto-generated method stub
		String sql = "select id,username,password from t_employee where username=? and password=?";
		return db.executeQueryForObject(sql, new UserRowMapper(),name, password);
		
	}
	
	private class UserRowMapper implements RowMapper<User> {

		public User mapperRow(ResultSet rs) throws SQLException {
			User user = new User();
			user.setId(rs.getString("id"));
			user.setUsername(rs.getString("username"));
			user.setPassword(rs.getString("password"));
			return user;
		}
		
		
		
	}

	public void deleteByUsername(String username) {
		String sql = "delete from t_employee where username=?";
		db.executeSQL(sql, username);
		
	}

	public User findByUsername(String username) {
		// TODO Auto-generated method stub
		String sql = "select id,username,password from t_employee where username=?";
		return db.executeQueryForObject(sql, new UserRowMapper(),username);
		
	}

	public List<User> findAll() {
		// TODO Auto-generated method stub
		String sql = "select id,username,password from t_employee";
		return db.executeQueryForList(sql,new UserRowMapper());
	}

}
